package com.zzg.service.impl;

import java.util.List;

import com.zzg.bean.Page;
import com.zzg.bean.User;
import com.zzg.dao.UserDAO;
import com.zzg.dao.impl.UserDAOImpl;
import com.zzg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDAO userDAO;
	
	@Override
	public List<User> findAllUser() {
 		return userDAO.findAllUser();
	}

	@Override
	public int addUser(User user) {
		return userDAO.addUser(user);
	}

	@Override
	public int updateUser(User user) {
		return userDAO.updateUser(user);
	}

	@Override
	public int deleteUser(int id) {
		return userDAO.deleteUser(id);
	}

	@Override
	public User findUserById(int id) {
		return userDAO.findUserById(id);
	}

	@Override
	public User selectUserbyUsername(String username) {
		return userDAO.selectUserbyUsername(username);
	}

	@Override
	public Page<User> selectUserPage(int currentPage) {
		//查询总行数
		int count = userDAO.selectUserCount();
		//计算出总页数
		int pageCount = count % Page.PAGE_SIZE == 0?count/Page.PAGE_SIZE:count/Page.PAGE_SIZE+1;
		//通过当前页数查询相应的分页数据（将数据包装到Page对象中）
		List<User> data = userDAO.selectUsersLimit((currentPage-1)*Page.PAGE_SIZE, Page.PAGE_SIZE);
		//返回Page对象
		Page<User> page = new Page<>();
		page.setCurrentPage(currentPage);
		page.setPageCount(pageCount);
		page.setData(data);
		return page;
	}

	@Override
	public Page<User> findUserLikeName(String username) {
		List<User> data = userDAO.findUserLikeName(username);
		Page<User> page = new Page<>();
		page.setData(data);
		return page;
	}

}
